package cn.cdiothree.service.impl;

import cn.cdiothree.dto.CartDto;
import cn.cdiothree.dao.CarouselMapper;
import cn.cdiothree.dataobject.Carousel;
import cn.cdiothree.dataobject.MyPageHelper;
import cn.cdiothree.dataobject.ProductInfo;
import cn.cdiothree.dao.ProductInfoMapper;
import cn.cdiothree.service.ProductInfoService;
import cn.cdiothree.enums.ResultEnum;
import cn.cdiothree.exception.SellException;
import cn.cdiothree.vo.CarouselVo;
import cn.cdiothree.vo.ProductDetailVo;
import cn.cdiothree.vo.ProductInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CumThing
 * @since 2020-02-02
 */
@Service
@Slf4j
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper,ProductInfo>implements ProductInfoService {

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Autowired
    private CarouselMapper carouselMapper;

    /**
     * 根据商品id查询商品
     * @param id
     * @return
     */
    @Override
    public ProductDetailVo findByProductId(Integer id) {
        log.info("查询商品{}信息",id);
        //传入商品信息
        ProductInfo productInfo= productInfoMapper.selectById(id);
        ProductDetailVo productDetailVO = new ProductDetailVo();
        BeanUtils.copyProperties(productInfo,productDetailVO);
        //传入轮播图图片
        QueryWrapper<Carousel> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",id);
        List<Carousel> carouselList =  carouselMapper.selectList(wrapper);

        List<CarouselVo> carouselVoList = new ArrayList<>();
        for (Carousel carousel:carouselList){
            CarouselVo carouselVO = new CarouselVo();
            carouselVO.setCarouselImage(carousel.getCarouselImage());
            carouselVoList.add(carouselVO);
        }
        productDetailVO.setCarouselVoList(carouselVoList);
        return productDetailVO;
    }


    @Override
    public ProductInfo findById(Integer id){
        ProductInfo productInfo = productInfoMapper.selectById(id);
        return productInfo;
    }

    /**
     * 查看所有商品信息
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public IPage<ProductInfo> findAll(int page,int pageSize) {
        log.info("查看所有商品信息");
        QueryWrapper<ProductInfo> productInfoQueryWrapper =new QueryWrapper<>();
        Page<ProductInfo> p=new Page<ProductInfo>(page,pageSize);
        return productInfoMapper.selectPage(p,productInfoQueryWrapper);

    }

    /**
     * 插入商品
     * @param productInfo
     */
    @Override
    public void insert(ProductInfo productInfo) {
        productInfoMapper.insert(productInfo);
    }

    /**
     * 增加商品库存
     * @param cartDtoList
     */
    @Override
    @Transactional(rollbackFor = SellException.class)
    public void increaseStock(List<CartDto> cartDtoList) {
        for (CartDto cartDto: cartDtoList){
            ProductInfo productInfo=productInfoMapper.selectById(cartDto.getId());
            if (productInfo==null){
                throw new SellException(ResultEnum.ORDER_NOT_EXIST);
            }
            Integer result=productInfo.getStock()+cartDto.getQuantity();
            productInfo.setStock(result);
            productInfoMapper.updateById(productInfo);
        }
    }

    /**
     * 减少商品库存
     * @param cartDtoList
     */
    @Override
    @Transactional(rollbackFor = SellException.class)
    public void decreaseStock(List<CartDto> cartDtoList) {
        for (CartDto cartDto: cartDtoList){
            ProductInfo productInfo=productInfoMapper.selectById(cartDto.getId());
            if (productInfo==null){
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }

            Integer result =productInfo.getStock()-cartDto.getQuantity();
            if (result<0){
                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
            }
            productInfo.setStock(result);
            productInfoMapper.updateById(productInfo);
        }
    }

    /**
     * 查询同一类目下所有商品信息
     * @param num
     * @param size
     * @param parentId
     * @return
     */
    @Override
    public MyPageHelper<ProductInfoVo> getProductInfoVosByParentId(int num, int size, Integer parentId) {
        log.info("查看类目{}下商品信息",parentId);
        QueryWrapper<ProductInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        PageHelper.startPage(num,size);
        List<ProductInfo> productInfoList = productInfoMapper.selectList(wrapper);
        PageInfo<ProductInfo> productInfoPageInfo=new PageInfo<>(productInfoList);
        List<ProductInfoVo> productInfoVoLists = productInfoList.stream().map(productInfo -> {
            return assembleProductInfoVO(productInfo);
        }).collect(Collectors.toList());
        MyPageHelper<ProductInfoVo> myPageHelper = new MyPageHelper<>();
        BeanUtils.copyProperties(productInfoPageInfo, myPageHelper);
        myPageHelper.setList(productInfoVoLists);
        return myPageHelper;
    }

    public ProductInfoVo assembleProductInfoVO(ProductInfo productInfo) {
        ProductInfoVo productInfoVO = new ProductInfoVo();
        BeanUtils.copyProperties(productInfo, productInfoVO);
        return productInfoVO;
    }
}
